package com.firebird.app.kotlin.stock.trend.view

import android.content.Context
import android.util.AttributeSet
import android.content.res.TypedArray
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.firebird.app.kotlin.stock.trend.R

class SimpleCommonTitleView : LinearLayout {
    private var leftButton: Button? = null
    private var rightButton: Button? = null
    private var titleTv: TextView? = null
    private var titleText: String? = null
    private var leftText: String? = null
    private var RightText: String? = null
    private var leftButVisable = false
    private var rightButVisable = false
    private var leftDrawableId = 0
    private var rightDrawableId = 0
    private var mContext: Context? = null

    constructor(context: Context?) : super(context, null) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        this.mContext = context
        //获取控件中自定义属性值
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.simpleCommonTitle)
        titleText = typedArray.getString(R.styleable.simpleCommonTitle_title_text)
        leftText = typedArray.getString(R.styleable.simpleCommonTitle_left_but_text)
        RightText = typedArray.getString(R.styleable.simpleCommonTitle_right_but_text)
        leftButVisable = typedArray.getBoolean(R.styleable.simpleCommonTitle_left_but_visable, false)
        rightButVisable = typedArray.getBoolean(R.styleable.simpleCommonTitle_right_but_visable, false)
        leftDrawableId = typedArray.getResourceId(R.styleable.simpleCommonTitle_left_but_drawable, -1)
        rightDrawableId = typedArray.getResourceId(R.styleable.simpleCommonTitle_right_but_drawable, -1)
        typedArray.recycle()
        //获取组合控件中的各个子控件
        View.inflate(context, R.layout.layout_simple_common_title, this)
        leftButton = findViewById<View>(R.id.title_left_but) as Button
        rightButton = findViewById<View>(R.id.title_right_but) as Button
        titleTv = findViewById<View>(R.id.title_tv) as TextView
        titleTv!!.text = titleText
        leftButton!!.text = leftText
        rightButton!!.text = RightText
        if (leftButVisable) {
            leftButton!!.visibility = View.VISIBLE
        }
        if (rightButVisable) {
            rightButton!!.visibility = View.VISIBLE
        }
        if (leftDrawableId != -1) {
            leftButton!!.setBackgroundResource(leftDrawableId)
        }
        if (rightDrawableId != -1) {
            rightButton!!.setBackgroundResource(rightDrawableId)
        }
    }

    /**
     * 设置左侧按钮的监听器
     * @param listener
     */
    fun setOnLeftClickListener(listener: OnClickListener?) {
        leftButton!!.setOnClickListener(listener)
    }

    /**
     * 设置右侧按钮的监听器
     * @param listener
     */
    fun setOnRightClickListener(listener: OnClickListener?) {
        rightButton!!.setOnClickListener(listener)
    }

    /**
     * 通过字符串资源设置title的文字内容
     * @param resId
     */
    fun setTitleText(resId: Int) {
        titleTv!!.text = context!!.getString(resId)
    }

    fun setTitleText(text: String?) {
        titleTv!!.text = text
    }
}